#!/usr/bin/python
import os


def touch(file_name):
    if file_name in os.listdir('.'):
        print("file exist!")
    else:
        print("creating %s" % file_name)
        fid = open(file_name, 'w')
        fid.close()


path = "/Users/liuweitao/Documents/"
date = "20241217"
logPath = path + date + "/"
files = os.listdir(logPath)
s = []
keword = "上报学习"
studentName = "黄影"
try:
    position = 0
    for file in files:
        if not os.path.isdir(file):
            if file == '.DS_Store':
                continue
            f = open(logPath + "/" + file)
            iter_f = iter(f)
            position += 1
            for line in iter_f:
                if line.find(keword) != -1 and line.find(studentName) != -1:
                    errorFile = path + "learn_"+str(position)+"_" + date + ".log"
                    print(errorFile)
                    if not os.path.exists(errorFile):
                        touch(errorFile)
                    logErrorFile = open(errorFile, "w")
                    logErrorFile.write(line)
                    logErrorFile.close()
            f.close()

    print(date + "分析日志文件数:" + str(len(files)))
finally:
    print(date + "分析日志文件数:" + str(len(files)))
